//
//  JBBasicDataTableViewModuleController.m
//  JustBen
//
//  Created by YongbinZhang on 10/29/13.
//  Copyright (c) 2013 ZhangYongbin(JustBen). All rights reserved.
//

#import "JBBasicDataTableViewModuleController.h"


static CGFloat const kBasicDataCellHeight = 40.0f;

static NSInteger const kDefaultLoadCount = 12;


@interface JBBasicDataTableViewModuleController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) JBBasicDataTableView *tableView;

@end



@implementation JBBasicDataTableViewModuleController

#pragma mark - init
- (id)init
{
    self = [super init];
    if (self) {
        _cellClassName = NSStringFromClass([JBBasicDataTableViewCell class]);
        _dataCountPerCell = 1;
        
        _tableView = [[JBBasicDataTableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [JBSystemInfo screenWidth], 0.0f)];
        _tableView.clipsToBounds = YES;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    
    return self;
}



#pragma mark - setters

- (void)setCellNibName:(NSString *)cellNibName
{
    if (cellNibName) {
        if (![cellNibName isEqualToString:_cellNibName]) {
            _cellNibName = [cellNibName copy];
            
            _cellClassName = [cellNibName copy];
        }
    }
}


#pragma mark - object methods

//  模版方法
//  重新加载数据
- (void)reloadData
{
    [self reloadDataWithSuccess:nil failure:nil];
}

- (void)reloadDataWithSuccess:(void (^)())success failure:(void (^)(NSInteger errorCode, NSString *errorMsg))failure
{
    [super reloadDataWithSuccess:success failure:failure];
}

- (void)reloadDataSuccessedWithLastPos:(NSString *)lastPos dataList:(NSArray *)dataList
{
    [super reloadDataSuccessedWithLastPos:lastPos dataList:dataList];
    [self.tableView reloadData];
}

- (void)reloadDataFailed
{
    [super reloadDataFailed];
    [self.tableView reloadData];
}


//  模版方法
//  加载更多的数据
- (void)loadMoreData
{
    [self loadMoreDataWithSuccess:nil failure:nil];
}

- (void)loadMoreDataWithSuccess:(void (^)())success failure:(void (^)(NSInteger errorCode, NSString *errorMsg))failure
{
    [super loadMoreDataWithSuccess:success failure:failure];
}

- (void)loadMoreDataSuccessedWithLastPos:(NSString *)lastPos dataList:(NSArray *)dataList
{
    [super loadMoreDataSuccessedWithLastPos:lastPos dataList:dataList];
    
    if (dataList && dataList.count > 0) {
        [self.tableView reloadData];
    }
}

- (void)loadMoreDataFailed
{
    [super loadMoreDataFailed];
}



//  模版方法
//  选中某一行，以该行对应的数据为参数
- (void)selectedCellAtIndexPath:(NSIndexPath *)indexPath
{
}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataCountPerCell == 1) {
        return self.dataList.count;
    } else {
        NSInteger row = self.dataList.count / self.dataCountPerCell;
        if (row * self.dataCountPerCell == self.dataList.count) {
            return row;
        } else {
            return row + 1;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifer;
    Class cellClass;
    
    if (self.cellNibName && self.cellNibName.length > 0) {
        cellIdentifer = self.cellNibName;
    } else if (self.cellClassName && self.cellClassName.length > 0) {
        cellIdentifer = self.cellClassName;
        cellClass = NSClassFromString(self.cellClassName);
    } else {
        cellIdentifer = @"JBBasicDataTableViewCell";
        cellClass = NSClassFromString(@"JBBasicDataTableViewCell");
    }
    
    
    JBBasicDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    
    if (!cell) {
        if (self.cellNibName) {
            cell = [[[NSBundle mainBundle] loadNibNamed:self.cellNibName owner:self options:nil] lastObject];
        } else {
            cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
        }
    }
    
    cell.indexPath = indexPath;
    cell.cellData = [self.dataList objectAtIndex:indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.cellClassName) {
        Class cellClass = NSClassFromString(self.cellClassName);
        if (cellClass) {
            return [cellClass cellHeightAtIndexPath:indexPath WithCellData:[self.dataList objectAtIndex:indexPath.row]];
        } else {
            return 44.0f;
        }
    } else {
        return 44.0f;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self selectedCellAtIndexPath:indexPath];
}


#pragma mark - scrollView delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.shouldReload) {
        //  TO DO
    }
    
    
    if ([self.delegateForTableView respondsToSelector:@selector(kaBasicDataTableViewModuleControllerTableViewIsScrolling:)]) {
        [self.delegateForTableView kaBasicDataTableViewModuleControllerTableViewIsScrolling:self];
    }
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (self.shouldReload) {
        //  TO DO
    }
    
    CGFloat contentOffsetY = scrollView.contentOffset.y;
    CGFloat contentHeight = scrollView.contentSize.height;
    CGFloat height = scrollView.bounds.size.height;
    
    if (contentOffsetY + height >= contentHeight) {
        if (self.shouldLoadMore) {
            [self loadMoreData];
        }
    }
}

@end
